package shaka.ui

import org.eclipse.swt.SWT
import org.eclipse.swt.events.MouseEvent
import org.eclipse.swt.graphics.Image
import org.eclipse.swt.graphics.Point
import org.eclipse.swt.layout.GridData
import org.eclipse.swt.widgets.Display
import org.eclipse.swt.widgets.Menu
import org.eclipse.swt.widgets.Shell
import org.eclipse.swt.widgets.Table
import org.eclipse.swt.widgets.TableItem

import shaka.groovy.swt.SwtBuilder

public class SwtApplication {
	static SwtBuilder swt
	static String fontType  = 'Consolas'
	static int    fontSize  = 12
	static def    fontStyle = SWT.NONE

	public static void main(String[] args) {
		FuncStorage.instance.tableInit()
		runApp()
	}

	static void runApp(){
		swt = new SwtBuilder()
		Display display = swt['display']
		Image icon = display.getSystemImage(SWT.ICON_INFORMATION)
		swt.shell(id:'shell', text:'Shaka 工具箱', image:icon, size:[1000, 600]){
			fillLayout()

			cTabFolder(c:[SWT.BOTTOM]){
				gridData(c:[GridData.FILL, GridData.FILL, true, true, 1, 1])
				SwtRegPaserPanel.panel(swt, fontType, fontSize, fontStyle)
				SwtCodeGenPanel.panel(swt, fontType, fontSize, fontStyle)
				SwtMklinkPanel.panel(swt, fontType, fontSize, fontStyle)
			}
			//pack()
		}
		swt.setLocation(swt['shell'], 0.3, 0.3)
		swt.show()
	}

	static void dialogSave(Closure c){
		// 輸入儲存名稱 dialog
		Shell dialog
		dialog = swt.shell(parent:swt['shell'], text:'請輸入儲存名稱'){
			gridLayout(c:[2, false])
			text(id:'storageSaveText', c:[SWT.BORDER]){
				font(c:[fontType, fontSize, fontStyle])
				gridData(prop:[widthHint:400])
			}
			button(c:[SWT.PUSH], text:'儲存'){
				font(c:[fontType, fontSize, fontStyle])
				event(click:{
					String key = swt['storageSaveText'].text
					c(key)
					dialog.close()
					dialog.dispose()
				})
			}
			pack()
		}
		dialog.open()
	}

	static void dialogGet(
			String func_name,
			Closure getCB={String value->},
			Closure saveCB={String key->},
			Closure renameCB={String key, String text->},
			Closure deleteCB={String key->}){
		Shell dialog
		dialog = swt.shell(parent:swt['shell'], size:[500, 400]){
			fillLayout()
			table(id:'storageTable', c:[SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION], headerVisible:true){
				tableColumn(c:[SWT.NONE], ['key', 'sort'])
				for(Map row : FuncStorage.instance.funcs(func_name)){
					tableItem(c:[SWT.NONE], [row['key'], String.valueOf(row['sort'])])
				}
				tableColumnW([300, 100])
				event(mouseup:{ MouseEvent evt ->
					Table table = swt['storageTable']
					TableItem[] items = table.getSelection()
					if(items.length == 0)
						return
					String key = items[0].getText(0)
					String value = FuncStorage.instance.func(func_name, key)
					switch(evt.button){
						case 1:
							getCB(value)
							break
						case 3:
							Menu menu = swt.menu(parent:dialog, c:[SWT.POP_UP]){
								menuItem(c:[SWT.PUSH], text:'取出'){
									event(click:{ getCB(value) })
								}
								menuItem(c:[SWT.PUSH], text:'儲存'){
									event(click:{ saveCB(key) })
								}
								menuItem(c:[SWT.PUSH], text:'修改'){
									event(click:{
										//println '2 ...'
										dialogSave({ String text ->
											renameCB(key, text)
											dialog.dispose()
										})
									})
								}
								menuItem(c:[SWT.PUSH], text:'刪除'){
									event(click:{
										deleteCB(key)
										dialog.dispose()
									})
								}
							}
							Point pt = new Point (evt.x, evt.y)
							pt = table.toDisplay (pt)
							menu.setLocation (pt.x, pt.y)
							menu.setVisible (true)
							break
					}
				})
			}
		}
		dialog.open()
	}
}
